﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;

namespace OurWedding1.Models
{
    public class TasksIndexViewModel
    {
        public IEnumerable<Task> Done { get; set; }
        public IEnumerable<Task> ThisWeek { get; set; }
        public IEnumerable<Task> ThisMonth { get; set; }
        public IEnumerable<Task> NextMonth { get; set; }
        public IEnumerable<Task> Later { get; set; }

        public TasksIndexViewModel(IQueryable<Task> tasks)
        {
            Done = tasks.Where(d => d.isDone).ToList();
            var now = DateTime.Now;
            DateTime start = now.Date.AddDays(-(int)now.DayOfWeek), // prev sunday 00:00 
                     end = start.AddDays(7); // next sunday 00:00
            var nextMonthNmb = now.AddMonths(1).Month;
            var laterStart = now.AddMonths(2).AddDays(-now.Day);
            ThisWeek = tasks.Where(d => d.Deadline.HasValue && !d.isDone).Where(d => /* d.Deadline >= start &&*/ d.Deadline < end).ToList();
            ThisMonth = tasks.Where(d => d.Deadline.HasValue).Where(d => d.Deadline.Value.Month == now.Month).ToList();
            NextMonth = tasks.Where(d => d.Deadline.HasValue).Where(d => d.Deadline.Value.Month == nextMonthNmb).ToList();
            Later = tasks.Where(d => d.Deadline.HasValue).Where(d => d.Deadline.Value >= laterStart).ToList();
        }

        public static int GetWeekNumber(DateTime date)
        {
            var ci = CultureInfo.CurrentCulture;
            var weekNum = ci.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
            return weekNum;
        }
    }

}